using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using SharpCore.Data;
using SharpCore.Extensions;
using SharpCore.Utilities;

namespace MMS.DAO
{
	public class UserDAO
	{
		#region Fields

		private string connectionStringName;

		#endregion

		#region Constructors

		public UserDAO(string connectionStringName)
		{
			ValidationUtility.ValidateArgument("connectionStringName", connectionStringName);

			this.connectionStringName = connectionStringName;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the User table.
		/// </summary>
		public void Insert(UserDTO user)
		{
			ValidationUtility.ValidateArgument("user", user);

			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@username", user.Username),
				new SqlParameter("@password", user.Password),
				new SqlParameter("@type", user.Type),
				new SqlParameter("@email", user.Email),
				new SqlParameter("@active", user.Active),
				new SqlParameter("@address", user.Address),
				new SqlParameter("@name", user.Name)
			};

			user.Id = (int) SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "UserInsert", parameters);
		}

		/// <summary>
		/// Updates a record in the User table.
		/// </summary>
		public void Update(UserDTO user)
		{
			ValidationUtility.ValidateArgument("user", user);

			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@id", user.Id),
				new SqlParameter("@username", user.Username),
				new SqlParameter("@password", user.Password),
				new SqlParameter("@type", user.Type),
				new SqlParameter("@email", user.Email),
				new SqlParameter("@active", user.Active),
				new SqlParameter("@address", user.Address),
				new SqlParameter("@name", user.Name)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "UserUpdate", parameters);
		}

		/// <summary>
		/// Deletes a record from the User table by its primary key.
		/// </summary>
		public void Delete(int id)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@id", id)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "UserDelete", parameters);
		}

		/// <summary>
		/// Selects a single record from the User table.
		/// </summary>
		public UserDTO Select(int id)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@id", id)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "UserSelect", parameters))
			{
				if (dataReader.Read())
				{
					return MapDataReader(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

		/// <summary>
		/// Selects a single record from the User table.
		/// </summary>
		public string SelectJson(int id)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@id", id)
			};

			return SqlClientUtility.ExecuteJson(connectionStringName, CommandType.StoredProcedure, "UserSelect", parameters);
		}

		/// <summary>
		/// Selects all records from the User table.
		/// </summary>
		public List<UserDTO> SelectAll()
		{
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "UserSelectAll"))
			{
				List<UserDTO> userDTOList = new List<UserDTO>();
				while (dataReader.Read())
				{
					UserDTO userDTO = MapDataReader(dataReader);
					userDTOList.Add(userDTO);
				}

				return userDTOList;
			}
		}

		/// <summary>
		/// Selects all records from the User table.
		/// </summary>
		public string SelectAllJson()
		{
			return SqlClientUtility.ExecuteJson(connectionStringName, CommandType.StoredProcedure, "UserSelectAll");
		}

		/// <summary>
		/// Creates a new instance of the UserDTO class and populates it with data from the specified SqlDataReader.
		/// </summary>
		private UserDTO MapDataReader(SqlDataReader dataReader)
		{
			UserDTO userDTO = new UserDTO();
			userDTO.Id = dataReader.GetInt32("id", 0);
			userDTO.Username = dataReader.GetString("username", null);
			userDTO.Password = dataReader.GetString("password", null);
			userDTO.Type = dataReader.GetString("type", null);
			userDTO.Email = dataReader.GetString("email", String.Empty);
			userDTO.Active = dataReader.GetBoolean("active", false);
			userDTO.Address = dataReader.GetString("address", null);
			userDTO.Name = dataReader.GetString("name", null);

			return userDTO;
		}

		#endregion
	}
}
